package com.ws.base.common.datadict.support;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.ws.base.common.WSAppLogger;
import com.ws.base.common.datadict.entity.Datadict;
import com.ws.base.common.datadict.entity.Datadictdetail;

public class DatadictTagSupport extends TagSupport {

    private static final long serialVersionUID = -3265126960480506989L;

    private WSAppLogger logger = WSAppLogger
            .getWSAppLogger(DatadictTagSupport.class);
    private String datadictid;
    private String typeid;

    @Override
    public int doStartTag() throws JspException {
        return EVAL_BODY_INCLUDE;
    }

    @Override
    public int doEndTag() throws JspException {
        String result = null;
        Datadict datadict = DatadictSupport.getInstance().getDatadictById(
                datadictid);

        if (null != datadict
                && datadict.getDatadictdetailMap().containsKey(typeid)) {
            Datadictdetail datadictdetail = datadict.getDatadictdetailMap()
                    .get(typeid);
            result = datadictdetail.getDescription();
        } else {
            result = "'" + typeid + "'";
        }

        try {
            pageContext.getOut().write(result);
        } catch (IOException e) {
            logger.error(e, "doEndTag error");
        }
        return SKIP_BODY;
    }

    public String getDatadictid() {
        return datadictid;
    }

    public void setDatadictid(String datadictid) {
        this.datadictid = datadictid;
    }

    public String getTypeid() {
        return typeid;
    }

    public void setTypeid(String typeid) {
        this.typeid = typeid;
    }

}
